//
//  ZTLCCameraUtil.m
//  YSSmartHome
//
//  Created by yanghao on 2016/9/28.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "ZTLCCameraUtil.h"
#import "CameraDeviceInfo.h"
#import "LiveVideoViewController.h"
#import "UIView+HUD.h"

static NSString *const svrStr = @"openapi.lechange.cn";
static NSInteger const iPort = 443;
static NSString *const appIdStr = @"lc1636ff487bc74a3e";
static NSString *const appSecretStr = @"46a77d711606499393bfe2634340e7";


@interface ZTLCCameraUtil ()
@end
@implementation ZTLCCameraUtil

+ (void)fetchToken:(NSString *)phoneNum CompletionBlock:(CompletionBlock)successBlock WithFailureBlock:(FailureBlock)failureBlock
{
    if (0 == phoneNum.length )
    {
        debugLogInfo(@"摄像头手机号码为空");
        return;
    }
    dispatch_queue_t enter_device = dispatch_queue_create("enter_device", nil);
    dispatch_async(enter_device, ^{
        NSString * acessTok;
        NSString * errCode;
        NSString * errMsg;
        OpenApiService *openApi = [[OpenApiService alloc] init];
        NSInteger ret = [openApi getUserToken:svrStr port:iPort appId:appIdStr appSecret:appSecretStr phone:phoneNum token:&acessTok errcode:&errCode errmsg:&errMsg];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (ret < 0)
            {
                if (![errCode isEqualToString:@"TK1004"])
                {
                    NSString *errMsg =@"当前手机号未与您的应用绑定，请点击\"用户绑定\"按扭进行用户绑定操作。";
                    debugLogInfo(@"%@",errMsg);
                    failureBlock(errCode);
                    return;
                }
                if (nil != errMsg)
                {
                    debugLogInfo(@"%@",[errMsg mutableCopy]);
                    failureBlock(errMsg);
                    return;
                }
            }
            else
            {
                NSString * userTokenStr = [acessTok copy];
                successBlock(userTokenStr);
            }
        });
        
    });
    
}

+ (LCOpenSDK_Api * )loadhc:(NSString *)token {
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"pem"];
    LCOpenSDK_Api *m_hc = [[LCOpenSDK_Api shareMyInstance]initOpenApi:svrStr port:iPort CA_PATH:cerPath];
    RestApiService * restApiService = [RestApiService shareMyInstance];
    if (nil != m_hc && nil != token)
    {
        [restApiService initComponent:m_hc Token:token];
        return m_hc;
    }else
    {
        NSLog(@"DeviceViewController, m_hc or m_accessToken is nil");
        return nil;
    }
}


+ (void)onLive:(ZTLCCamera *)lcCamera controller:(UIViewController *)controller
{
    CameraDeviceInfo *carmeraDeviceInfo = lcCamera.cameraDeviceInfo;
    if (carmeraDeviceInfo == nil) {
        [[UIApplication sharedApplication].windows.lastObject yh_showMsgHUD:@"设备正在初始化，请稍后再试"];
        return;
    }
    NSString * m_strDevSelected = (carmeraDeviceInfo.devInfo)->ID;
    NSString *m_devAbilitySelected = (carmeraDeviceInfo.devInfo)->ability;
    NSInteger m_devChnSelected = (carmeraDeviceInfo.devInfo)->channelId[carmeraDeviceInfo.index];
    NSString *m_accessToken =[carmeraDeviceInfo token];
    debugLogInfo(@"onLive device[%@],channel[%ld]",m_strDevSelected,(long)m_devChnSelected);
    UIStoryboard *currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LiveVideoViewController *liveVideoView = [currentBoard instantiateViewControllerWithIdentifier:@"LiveVideo"];
    liveVideoView.navigationItem.title  = [lcCamera getDeviceName];
    [controller.navigationController pushViewController:liveVideoView animated:NO];
    [liveVideoView setInfo:m_accessToken Dev:m_strDevSelected Chn:m_devChnSelected PicUrl:carmeraDeviceInfo.picUrl Abl:m_devAbilitySelected];
}

+ (BOOL)unbundling:(NSString *)serial
{
    BOOL result = false;
    NSString * destOut;
    RestApiService * restApiService = [RestApiService shareMyInstance];
    BOOL ret =  [restApiService unBindDevice:serial Desc:&destOut];
    debugLogInfo(@"destOut = %@", destOut);
    if (ret ) {
        debugLogInfo(@"摄像头解绑成功！");
    }else{
        debugLogInfo(@"摄像头解绑失败！");
    }
    return result;
}
@end
